package com.example.loginandpaytools;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.example.loginandpaytools.Support.LoginEvent;
import com.example.loginandpaytools.Support.RxBus;
import com.example.loginandpaytools.Utils.HttpCallbackListener;
import com.example.loginandpaytools.Utils.HttpUtil;
import com.example.loginandpaytools.Utils.LoginCallbackListener;
import com.example.loginandpaytools.common.Config;
import com.example.loginandpaytools.ui.LoginDialogActivity;
import com.example.loginandpaytools.ui.floatingbutton.FabPresenter;
import com.example.loginandpaytools.ui.floatingbutton.FloatingButtonContract;
import com.example.loginandpaytools.ui.postorder.PostOrderActivity;

import rx.functions.Action1;

/**
 * Created by jerry on 17-4-7.
 */

public class LoginAndPayToolSDK {
    /**
     * 打开登陆界面
     * @param activity
     */
    public static void openLoginInterface(Context activity){

        Intent intent = LoginDialogActivity.newIntent(activity);
        activity.startActivity(intent);
    }



    /**
     * 处理登陆事件
     * @param listener
     */
    public static void handleLoginEvent( final LoginCallbackListener listener){
        RxBus.getDefault().toObservable(LoginEvent.class)
                .subscribe(new Action1<LoginEvent>() {
                    @Override
                    public void call(LoginEvent loginEvent) {
                        String code = loginEvent.getLoginCode();
                        //Toast.makeText(MainActivity.this, code, Toast.LENGTH_SHORT).show();
                        listener.succeedLogin(code);
                    }
                });

    }


    /**
     * 激活接口
     * @param listener
     * @param context
     */
    public static void  activeInterface(Context context, HttpCallbackListener listener){

        HttpUtil.activeInterface(context, listener);

    }

    /**
     * 角色登陆接口
     * @param listener
     * @param activity
     */
    public static void roleLogin(final Activity activity, HttpCallbackListener listener){
        HttpUtil.roleLogin(activity, listener);

    }
    /**
     * 打开支付页面
     * @param activity
     * @param title
     */
    public static void openPayInterface(Activity activity, String title) {
        Intent intent = new Intent(activity, PostOrderActivity.class);
        intent.putExtra(PostOrderActivity.TOOLBAR_TITLE, title);//传入显示的标题
        activity.startActivity(intent);
    }

    /**
     * 调用是否打开悬浮按钮接口
     * @param activity
     * @param view
     */
    public static void openFabButton(Activity activity, FloatingButtonContract.View view) {
        FloatingButtonContract.Presenter presenter = new FabPresenter(view);
        if (presenter != null) {
            presenter.setmContext(activity);
        }

        if (Config.fab_token == null) {
            Toast.makeText(activity, "请先登录", Toast.LENGTH_SHORT).show();
        } else {
            presenter.getFabResponse();
        }
    }



}
